Hello guys!
This article is the seventh part of this article series. This article explains lists and tuples in Python, the related operations and the functioning in detail using several examples and graphics. So let's explore it.
Getting Theme
To get the theme of Python, kindly go through my previous articles:
Lists
Lists are one of the most valuable Data Types in programming language and so they are in Python. One can easily see that lists are somehow similar to arrays but still, there are a huge difference between both of these data types, which are that an array contains homogeneous (of the same data type) items whereas lists contain heterogeneous (of various data types) items. This can also be considered to be an advantage of lists over arrays.
Now let's get back to lists. Lists contain items separated the commas and enclosed in brackets "[ ]". The values stored in a list can be accessed using "[:]" (depending on their respective index).
Structure
A simple structure of accessing elements using indexes is:
Print list[index]
Example
Now let's go through some examples.
- list = ['Flipkart', 'Amazon', 'SnapDeal', 'Myntra', 'Junglee']
-
-
-
- print (list)
Output- ['Flipkart', 'Amazon', 'SnapDeal', 'Myntra', 'Junglee']
Output- Flipkart
Output- ['Amazon', 'SnapDeal', 'Myntra']
Output- ['SnapDeal', 'Myntra', 'Junglee']
(Now let's have a look at some of the operations over lists).
Output Window
List | Concatenation
You can concatenate elements of two lists using the "+" operator as we do in strings.
For Example:
- list1 = ['Flipkart', 'Amazon', 'SnapDeal']
-
- list2 = ['Myntra', 'Junglee']
-
-
-
- print (list1 + list2)
Output- ['Flipkart', 'Amazon', 'SnapDeal', 'Myntra', 'Junglee']
Output Window
List | Repetition
For repeating a list's elements twice or more we can use the "*" operator in the same fashion as we do in strings.
For Example:
- list1 = ['Flipkart', 'Amazon', 'SnapDeal']
-
- list2 = ['Myntra', 'Junglee']
-
-
-
- print (2 * list2)
Output- ['Myntra', 'Junglee', 'Myntra', 'Junglee']
Output Window
Tuples
A tuple is another data type in Python that is similar to a list in some contexts. A tuple also contains a set of elements separated by a comma and enclosed between a pair of brackets "()".
The values stored in a list can be accessed using "[:]" the same as a list depending on their respective index.
Structure
A simple structure of accessing elements using indexes is:
print tuple[index]
Example
Let's go through some examples to have a better understanding:
- tuple = ('Flipkart', 'Amazon', 'SnapDeal', 'Myntra', 'Junglee')
-
-
-
- print (tuple)
Output- ('Flipkart', 'Amazon', 'SnapDeal', 'Myntra', 'Junglee')
Output- Flipkart
Output- ('Amazon', 'SnapDeal', 'Myntra')
Output- ('SnapDeal', 'Myntra', 'Junglee')
(Now let's move to some operations.)
Output Window
Tuple | Concatenation
You can concatenate elements of two tuples using the "+" operator as we do in strings as well as in lists.
For Example:
- tuple1 = ('Flipkart', 'Amazon', 'SnapDeal')
-
- tuple2 = ('Myntra', 'Junglee')
-
-
-
- print (tuple1 + tuple2)
Output- ('Flipkart', 'Amazon', 'SnapDeal', 'Myntra', 'Junglee')
Output Window
Tuple | Repetition
For repeating a list's elements twice or more we can use the "*" operator in the same fashion as we were doing in strings as well as in lists.
For Example:
- tuple1 = ('Flipkart', 'Amazon', 'SnapDeal')
-
- tuple2 = ('Myntra', 'Junglee')
-
-
-
- print (2 * tuple2)
Output- ('Myntra', 'Junglee', 'Myntra', 'Junglee')
Output Window
PinPoints
Guys, perhaps you are thinking that lists and tuples look similar in function and are wondering how to differentiate them.
Let me provide you some pinpoints to get a clear view of them:
- Elements in Lists are enclosed between the brackets []
- Elements in Tuples are enclosed between parentheses ()
- Size and elements in a list can be changed
- Size and elements in a tuple can't be changed (Read-Only*)
(Still, if you guys didn't get the proper theme of tuples then don't worry, in my next article I'll explain tuples in greater detail.)
Guidelines from my Side
The following are some guidelines from my side:
- Do as much code as you can.
- Code anything you want, that is the best way to learn
- Don't just study things, try to learn them.
- Work on your concepts
- Work on the fundamentals of any technology or stuff you want to learn.
Moto
“Keep calm and code Python”.
I tried to make this an interesting and interactive article and I hope you guys like it, meanwhile if you have any suggestions then your welcome.
Until the next part, keep sharing!
Happy Learning!